function ti = sethandle(ti, h)
% transimg::sethandle  - set handle

% global variables for storage
global tiobj ...
       tiobjlup;
   
% check arguments
lup = find(tiobjlup == ti.L);
if numel(lup) ~= 1
    error( ...
        'transimg:ObjectRemoved', ...
        'Object removed from global storage.' ...
    );
end
if nargin < 2 || ...
   ~isa(h, 'double') || ...
    numel(h) ~= 1 || ...
   ~ishandle(h) || ...
   ~strcmpi(get(h, 'type'), 'image')
    error( ...
        'transimg:InvalidCall', ...
        'No image handle provided.' ...
    );
end

% set handle
tiobj(lup).Handle = h;

% set axes properties correctly
x = get(h, 'Parent');
set(x, ...
    'XLim', [0.5, 0.5 + tiobj(lup).Width], ...
    'YLim', [0.5, 0.5 + tiobj(lup).Height]);

% if rendered, set CData
if tiobj(lup).IsRendered
    set(h, 'CData', tiobj(lup).Rendered);
end
